define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var openLayui = function (id){
        Fast.api.layer.open({
            type:2,
            area:["80%","80%"],
            content:'vip/expires/index?ids='+id,
        });
    };

    var Controller = {
        index: function () {


            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    //add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    //del_url: 'user/user/del',
                    //multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true,visible:false},
                        // {field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'open_id', title: '微信open_id', operate: 'LIKE'},
                        // {field: 'invite_count', title: '邀请人数', operate: 'LIKE'},
                        // {field: 'invite_auth_count', title: '有效认证人数', operate: 'LIKE'},
                        // {field: 'todayInviteCount', title: '今日邀请人数', operate: false},
                        {field: 'invite_code', title: '邀请码', operate: false,formatter: function (value,row,index){
                            if (row.invite_code){
                                return '<button onclick="openLayuiInvite('+row.id+')">'+row.invite_code+'</button></br><span style="font-size:10px;" >今：'+row.todayInviteCount+' &nbsp;&nbsp;总：'+row.invite_count+' &nbsp;&nbsp;认证：'+row.invite_auth_count+'</span>';
                            }else{
                                return '<button onclick="openLayuiInvite('+row.id+')">点击查看</button></br><span style="font-size:10px;">今：'+row.todayInviteCount+' &nbsp;&nbsp;总：'+row.invite_count+' &nbsp;&nbsp;认证：'+row.invite_auth_count+'</span>';
                            }

                            }},
                        {field: 'is_authentication', title: '实名认证', formatter: Table.api.formatter.status, searchList: {"0": '否', "1": "是"}},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'levelName', title: __('Level'), operate: false},
                        {field: 'temp_level_name', title:'临时等级', operate: false,formatter: function (value,row,index){
                            if (row.temp_level){
                                return '<button onclick="openLayui('+row.id+')">'+row.temp_level_name+'</button>';
                            }else{
                                return '<button onclick="openLayui('+row.id+')">点击赠送</button>';
                            }
                            }},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                {
                                    name:'助教订单',
                                    extend:'data-area=["90%","90%"]',
                                    title:'助教订单',
                                    text:'助教订单',
                                    icon:'fa fa-magic',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'user/assistantorder/index',
                                    refresh:true
                                },
                                {
                                    name:'优惠券',
                                    extend:'data-area=["90%","90%"]',
                                    title:'优惠券',
                                    text:'优惠券',
                                    icon:'fa fa-magic',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'user/coupon/index?type=1',
                                    refresh:true
                                },
                                {
                                    name:'提现记录',
                                    extend:'data-area=["90%","90%"]',
                                    title:'提现记录',
                                    text:'提现记录',
                                    icon:'fa fa-magic',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'user/withdrawrecord/index',
                                    refresh:true
                                },
                                {
                                    name:'用户账户',
                                    extend:'data-area=["60%","90%"]',
                                    title:'用户账户',
                                    icon:'fa fa-magic',
                                    text:'用户账户',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'user/account/edit',
                                    refresh:true
                                },
                                {
                                    name:'实名认证',
                                    extend:'data-area=["50%","50%"]',
                                    title:'实名认证',
                                    icon:'fa fa-magic',
                                    text:'实名认证',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'user/authentication/show',
                                    refresh:true,
                                    visible:function(value){
                                        if (value.is_authentication==1){
                                            return true;
                                        }
                                    }
                                },

                                {
                                    name:'余额记录',
                                    extend:'data-area=["90%","90%"]',
                                    title:'余额记录',
                                    icon:'fa fa-magic',
                                    text:'余额记录',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url:'user/moneylog/index',
                                    refresh:true
                                },
                                {
                                    name:'积分记录',
                                    title:'积分记录',
                                    icon:'fa fa-magic',
                                    text:'积分记录',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url:'user/scorelog/index',
                                    refresh:true
                                },
                                {
                                    name:'返点记录',
                                    title:'返点记录',
                                    icon:'fa fa-magic',
                                    text:'返点记录',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url:'user/rebate/index',
                                    refresh:true
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});